import requests
import json
import os

# 设置默认的 OpenAI API URL
openai_base_url = os.getenv('OPENAI_BASE_URL', 'https://api.openai.com/v1')
print(f"使用 OpenAI Base URL: {openai_base_url}")
url = openai_base_url + "/chat/completions"
payload = json.dumps({
    "model": "gpt-4o-mini",
    "messages": [
        {"role": "system", "content": "assistant"},
        {"role": "user", "content": "Hello world"}
    ]
})
# 检查 API Key 是否存在
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
    print("错误：未找到 OPENAI_API_KEY 环境变量")
    exit(1)

headers = {
    'Accept': 'application/json',
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + api_key,
    'Host': 'api.openai.com',
    'Connection': 'keep-alive'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
